﻿using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using CRMBiz;
using CRMEntity;

namespace CRMWeb.Utilities.Context
{
    public class CMSContext
    {
        protected static ContextData ContextData
        {
            get
            {
                var contextData = (ContextData)AbstractStockHelper<RequestStockHelper>.GetItem("CurrentContextData", true);
                if (contextData == null)
                {
                    contextData = new ContextData();
                    AbstractStockHelper<RequestStockHelper>.Add("CurrentContextData", contextData, true);
                }
                return contextData;
            }
        }

        public static CmsMember CurrentUser
        {
            get
            {
                return ContextData.CurrentUser;
            }
            set
            {
                ContextData.CurrentUser = value;
            }
        }

        public static List<CmsMemberGroup> CurrentRoles
        {
            get
            {
                return ContextData.CurrentRoles;
            }
            set
            {
                ContextData.CurrentRoles = value;
            }
        }

        public static void AuthenticateUser(string userName, bool createPresistentCookie)
        {
            var user = CmsMemberHandler.GetByUserName(userName);
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(userName, createPresistentCookie);
                ContextData.SetCurrentUser(user);
            }
        }

        public static void LogoutUser()
        {
            FormsAuthentication.SignOut();
            SessionHelper.Abandon();
            CurrentUser = null;
            HttpContext.Current.Response.Cache.SetNoStore();
        }
        
        public static bool IsAuthenticated()
        {
            //return UserInfoProvider.IsAuthenticated();
            return HTTPHelper.IsUserAuthenticated();
        }

    }
}